package com.aimao.blog.controller;

import com.aimao.blog.constant.ServerConst;
import com.aimao.blog.constant.ServerResponse;
import com.aimao.blog.pojo.Comment;
import com.aimao.blog.pojo.Subject;
import com.aimao.blog.pojo.Tag;
import com.aimao.blog.service.*;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author PuaChen
 * @Create 2018-07-06 21:28
 */
@RestController
@RequestMapping("/front")
public class FrontController {

    @Autowired
    private AccountAdminService accountAdminService;

    @Autowired
    private ArticleAdminService articleAdminService;

    @Autowired
    private SubjectAdminService subjectAdminService;

    @Autowired
    private CommentAdminService commentAdminService;

    @Autowired
    private SortAdminService sortAdminService;

    @Autowired
    private KeyWordUseCount keyWordUseCount;

    @Autowired
    private TagAdminService tagAdminService;

    @Autowired
    private BannerService bannerService;

    ///////////////////////////////////////////////////////////////////////////
    // Home 首页
    ///////////////////////////////////////////////////////////////////////////

    @RequestMapping("/get-admin")
    public ServerResponse getAdmin() {
        return ServerResponse.createSuccessAndData(accountAdminService.getAdminBean(ServerConst.ADMIN_KEY));
    }

    @RequestMapping("/get-top-article")
    public ServerResponse getTopArticle() {
        return articleAdminService.getArticleTop();
    }

    @RequestMapping("/get-article-new-all")
    public ServerResponse findArticleAll(boolean isSplit, Integer page, Integer size) {
        return articleAdminService.findALl(isSplit, page, size);
    }

    @RequestMapping("/get-tag-hot")
    public ServerResponse getHotTagAll() {
        ServerResponse<IPage<Tag>> all = tagAdminService.findAll(true, 1, 20);
        List<Tag> list = new ArrayList<>();
        if (all.isSuccess()) {
            list = all.data.getRecords();
        }
        return ServerResponse.createSuccessAndData(list);
    }

    /**
     * 获取所有的分类
     *
     * @return
     */
    @RequestMapping("/get-sort-all")
    public ServerResponse getSortAll() {
        return sortAdminService.findAll(false, null, null);
    }


    @RequestMapping("/get-article-detail")
    public ServerResponse getArticleDetail(Integer id) {
        if (id == null) {
            return ServerResponse.createFailure("参数错误");
        }
        return articleAdminService.getArticleDetail(id);
    }

    @RequestMapping("/get-subject-hot")
    public ServerResponse getHotSubject() {
        return subjectAdminService.findHostSubjectAll();
    }

    @RequestMapping("/get-comment-article")
    public ServerResponse getCommentByArticleId(Integer id, boolean isSplit, Integer page, Integer size) {
        if (id == null) {
            return ServerResponse.createFailure("参数错误");
        }
        return commentAdminService.findAll(id, isSplit, page, size);
    }

    @RequestMapping("/reply-comment")
    public ServerResponse replyComment(Comment comment) {
        if (comment == null || comment.getArticleid() == null) {
            return ServerResponse.createFailure("参数错误");
        }
        comment.setStatus(false);
        comment.setReadstate(false);
        return commentAdminService.replyComment(comment.getParentid(), comment);
    }


    ///////////////////////////////////////////////////////////////////////////
    // 首页Banner
    ///////////////////////////////////////////////////////////////////////////


    /**
     * 获取首页BannerList列表
     *
     * @return
     */
    @RequestMapping("/getBannerList")
    public ServerResponse getBannerList() {
        return bannerService.selectBannerList();
    }

    ///////////////////////////////////////////////////////////////////////////
    // 主题API
    ///////////////////////////////////////////////////////////////////////////


    @RequestMapping("/get-subject-all")
    public ServerResponse getSubjectListAll() {
        ServerResponse<IPage<Subject>> all = subjectAdminService.findAll(false, 0, -100);
        List<Subject> list = new ArrayList<>();
        if (all.isSuccess()) {
            list = all.data.getRecords();
        }
        return ServerResponse.createSuccessAndData(list);
    }

    @RequestMapping("/get-article-ids")
    public ServerResponse getArticleByIds(String ids, boolean isSplit, Integer page, Integer size) {
        if (StringUtils.isBlank(ids)) {
            return ServerResponse.createFailure("参数错误");
        }
        List<Integer> list = null;
        try {
            list = JSONArray.parseArray(ids.replaceAll("\\\"", ""), Integer.class);
        } catch (Exception e) {
            return ServerResponse.createFailure("转换异常");
        }
        return articleAdminService.findToIdList(list, isSplit, page, size);
    }

    @RequestMapping("/get-subject-id")
    public ServerResponse getSubjectById(Integer id) {
        if (id == null) {
            return ServerResponse.createFailure("参数错误");
        }
        return subjectAdminService.findById(id);
    }

    ///////////////////////////////////////////////////////////////////////////
    // 搜索API
    ///////////////////////////////////////////////////////////////////////////

    @RequestMapping("/search-article")
    public ServerResponse searchArticle(String key, boolean isSplit, Integer page, Integer size) {
        if (StringUtils.isBlank(key)) {
            return ServerResponse.createFailure("参数错误");
        }
        //统计用户搜索的关键字
        keyWordUseCount.addKeyWord(key);
        return articleAdminService.findToLike(key, isSplit, page, size);
    }

    /**
     * 获取统计用户搜索的最热门词汇
     *
     * @param size
     * @return
     */
    @RequestMapping("/get-search-key-count")
    public ServerResponse getKeyWordSearchCount(@RequestParam(required = false, defaultValue = "10", name = "size") Integer size) {
        return ServerResponse.createSuccessAndData(keyWordUseCount.getHotList(size));
    }

    @RequestMapping("/update-article-read")
    public ServerResponse updateArticleReadCount(Integer id) {
        if (id == null) {
            return ServerResponse.createFailure("参数错误");
        }
        return articleAdminService.updateArticleReadCount(id);
    }

    @RequestMapping("/get-article-sort-id")
    public ServerResponse getArticleBySortId(Integer id, boolean isSplit, Integer page, Integer size) {
        if (id == null) {
            return ServerResponse.createFailure("参数错误");
        }
        return articleAdminService.findAllBySortId(id, isSplit, page, size);
    }

}
